import { _decorator, Component, EventTouch, Node, Sprite, SpriteFrame } from 'cc';
import { LoadingManager } from '../loading/LoadingManager';
const { ccclass, property } = _decorator;

@ccclass('WeaponShowManager')
export class WeaponShowManager extends Component {

    @property(Node)
    private junHuoBtn:Node;

    @property(Node)
    private radarbtn:Node;

    @property(Node)
    private shenYoubtn:Node;

    @property(Node)
    private exiteBtn:Node;

    @property(Node)
    private junHuoPage:Node;

    @property(Node)
    private radarPage:Node;

    @property(Node)
    private baolaiwuPage:Node;


    @property(Node)
    private leftIconNode:Node;

    @property(Node)
    private rightIconNode:Node;


    @property(SpriteFrame)
    private selectedBg:SpriteFrame;

    @property(SpriteFrame)
    private defBg:SpriteFrame;

    @property(Node)
    private showPageRootNode:Node;

    start() {
        //this.node.getChildByName("bg").getComponent(Sprite).spriteFrame = LoadingManager.bigFrames.get("weapon_bg");
        
        
        this.exiteBtn.on(Node.EventType.TOUCH_END, (eventTouch:EventTouch) => {
            
            this.processBgClick(null);
            this.node.setPosition(1000, 0, 0);
            this.node.active = false;
        });

        this.junHuoBtn.on(Node.EventType.TOUCH_END, (eventTouch:EventTouch) => {
            
            this.processBgClick(eventTouch.target);

            this.junHuoPage.active = true;
            this.junHuoPage.setPosition(0, 75, 0);
        });

        this.shenYoubtn.on(Node.EventType.TOUCH_END, (eventTouch:EventTouch) => {

            this.processBgClick(eventTouch.target);

            this.baolaiwuPage.active = true;
            this.baolaiwuPage.setPosition(0, 75, 0);
        });

        this.radarbtn.on(Node.EventType.TOUCH_END, (eventTouch:EventTouch) => {

            this.processBgClick(eventTouch.target);    
            
            this.radarPage.active = true;
            this.radarPage.setPosition(0, 75, 0);
        });

        this.closeEvent(this.junHuoPage);
        this.closeEvent(this.radarPage);
        this.closeEvent(this.baolaiwuPage);
    }

    private closeEvent(node:Node) {
        node.getChildByName("close").on(Node.EventType.TOUCH_END, (event:EventTouch) => {
            const node = event.target as Node;
            node.parent.active = false;

            this.processBgClick(null);
        });
    }

    private processBgClick(target:Node) {
        this.exiteBtn.getComponent(Sprite).spriteFrame = this.defBg;
        this.junHuoBtn.getComponent(Sprite).spriteFrame = this.defBg;
        this.shenYoubtn.getComponent(Sprite).spriteFrame = this.defBg;
        this.radarbtn.getComponent(Sprite).spriteFrame = this.defBg;

        if (target) {
            target.getComponent(Sprite).spriteFrame = this.selectedBg;
        }

        this.showPageRootNode.children.forEach(p => {
            p.active = false;
        });
    }

    public showPage() {
        this.node.setPosition(0, 0, 0);
        this.node.active = true;
    }

    update(deltaTime: number) {
        
    }
}

